﻿app.filter('startFrom', function () {
    return function (input, start) {
        if (input) {
            start = +start; //parse to int
            return input.slice(start);
        }
        return [];
    }
});
app.controller('UserSettingsController', ['$scope', 'GlobalService', 'ApplicationService', 'alertboxservice', 'fileUpload', function ($scope, GlobalService, ApplicationService, alertboxservice, fileUpload) {

    $scope.toggle = function () {
        $("#UserSettings").modal({ backdrop: 'static' });
        $scope.title = "Edit User Settings";
        GlobalService.GetSettings().success(function (data) {
            $scope.settings = data;
        });
    }

    $scope.changePassword = function () {
        url = "../UserSettings/ChangePassword";
        ApplicationService.Save($scope.User, url, "Password").success(function (data) {
            alertboxservice.ShowMsg(data, "UserSettings ");
            BindSettings();
        });
    }

    $scope.save = function () {
        url = "../UserSettings/SaveDefaultSettings";
        ApplicationService.Save($scope.settings, url, "UserSettings").success(function (result) {
            alertboxservice.ShowMsg(result, "UserSettings ");
        });
    }

    GlobalService.GetCountry().success(function (data) {
        $scope.countries = data;
    });

    GlobalService.GetLanguages().success(function (data) {
        $scope.Languages = data;
    });

    GlobalService.GetDateformat().success(function (data) {
        $scope.Dateformat = data;
    });

    BindSettings();

    function BindSettings()
    {
        GlobalService.GetSettings().success(function (data) {
            $scope.settings = data;
        });
    }
    
    $scope.fileChangedProfile = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.settings.UserPicture = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    $scope.fileChangedCover = function (evt) {
        var file = evt.currentTarget.files[0];
        var reader = new FileReader();
        reader.onload = function (evt) {
            $scope.$apply(function ($scope) {
                $scope.settings.CoverPicture = evt.target.result;
            });
        };
        reader.readAsDataURL(file);
    }

    
    $scope.$watch('settings.CountryId', function (newVal) {
        GlobalService.GetStates(newVal).success(function (data) {
            $scope.states = data;
        });
    });

}]);

